// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Global = require('global');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        bird:{
            type:cc.Sprite,
            default:null,
        },
        upAndDown:{
            type:cc.Prefab,
            default:null,
        },
        mountain_1:{
            type:cc.Sprite,
            default:null,
        },
        mountain_2:{
            type:cc.Sprite,
            default:null,
        },
        cloud_1:{
            type:cc.Sprite,
            default:null,
        },
        cloud_2:{
            type:cc.Sprite,
            default:null,
        },
        quick:{
            type:cc.Prefab,
            default:null,
        },
        upAndDownList:[],
        updateIndex:0,
		oldSpeed:1,
     },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 开启碰撞检测
        cc.director.getCollisionManager().enabled=true;
        
        //this.init();
    },
    init(){

    },
    start () {
        

    },
    update (dt) {
        //山
        this.mountain_1.node.x>-540?this.mountain_1.node.x-=0.5:this.mountain_1.node.x=540;
        this.mountain_2.node.x>-540?this.mountain_2.node.x-=0.5:this.mountain_2.node.x=540;

        //云朵1
        this.cloud_1.node.x>-280?this.cloud_1.node.x-=0.1:this.cloud_1.node.x=280;
        
        //云朵2
        this.cloud_2.node.x>-280?this.cloud_2.node.x-=0.2:this.cloud_2.node.x=280;

        if(Global.isStart&&!Global.isOver){
            //柱子 todo:突然提速引起柱子间距太近的问题
            if(this.updateIndex==0||this.updateIndex%(Math.ceil(170/Global.speed))==0){
				//加速延迟
				if(this.oldSpeed!=Global.speed){
					this.oldSpeed = Global.speed;
					return;
				}
                var upAndDown = cc.instantiate(this.upAndDown);
                upAndDown.y = -70+Math.ceil(Math.random()*140);
                console.log("生成柱子:x:"+upAndDown.x+",y:"+upAndDown.y);
                this.node.addChild(upAndDown);
                //this.node.zIndex = 1;
                this.upAndDownList.push(upAndDown);
            }
            for(var i=0;i<this.upAndDownList.length;i++){
                this.upAndDownList[i].x-=Global.speed*2;
            }

            //加速
            if (!this._quick&&this.updateIndex%500==0) {
                this._quick = cc.instantiate(this.quick);
                this._quick.y = 50+Math.ceil(Math.random()*50);
                this._quick.zIndex = 2;
                this.node.addChild(this._quick);
            }
            this._quick&&this._quick.x--;
            if(this._quick&&this._quick.x<-300){
                this._quick.removeFromParent();
                this._quick = null;
            }

        }

        this.updateIndex++;
    }
});
